package com.boarsoft.boar.demo.user.biz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.demo.user.entity.PocAccount;
import com.boarsoft.boar.demo.user.entity.PocUser;
import com.boarsoft.cache.aop.Cached;
import com.boarsoft.dao.sharding.bean.DaoSharding;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("userBiz")
public class PocUserBizImpl extends SimpleBizImpl implements PocUserBiz {

	@Autowired
	PocAccountBiz accountBiz;

	@Override
	@Transactional
	@DaoSharding(policy = "tenant", index = 0)
	public boolean add(int tenantId, PocUser user) {
		// 创建用户
		if (dao.save(user) == null) {
			return false;
		}
		// 创建账号
		PocAccount account = new PocAccount();
		account.setUserId(user.getUserId());
		account.setAvailableAmount(10000);
		account.setLockAmount(0);
		account.setTenantId(tenantId);
		account.setCreateTime(user.getCreateTime());
		account.setModifyTime(user.getModifyTime());
		if (dao.save(account) == null) {
			return false;
		}
		return true;
	}

	@Override
	@Transactional(readOnly = true)
	@Cached(group = "user", index = 0)
	@DaoSharding(policy = "tenant", index = 0)
	public PocUser findById(int tenantId, int userId) {
		String hql = "from PocUser where userId=" + userId;
		return (PocUser) dao.find(hql);
	}

}
